﻿Imports System.DirectoryServices
Imports System.Windows.Forms
Public Class Utiles

    Public Function autenticarUsuarioLDAP(
        ByVal path As String,
        ByVal dominio As String,
        ByVal usuario As String,
        ByVal contrasena As String) As Boolean

        Dim entry As DirectoryEntry =
            New DirectoryEntry(path & dominio,
                               dominio & "\" & usuario,
                               contrasena)

        Try
            'creamos un objeto nativo pra forzar la autenticación
            Dim obj As Object = entry.NativeObject
            Dim search As DirectorySearcher = New DirectorySearcher(entry)

            search.Filter = "(SAMAccountName=" & usuario & ")"
            search.PropertiesToLoad.Add("cn")
            Dim result As SearchResult = search.FindOne()


            If (result Is Nothing) Then
                Return False
            End If

            'muestra el path del usuario en el directorio
            'MsgBox(result.Path)
            'MsgBox(CType(result.Properties("cn")(0), String))

        Catch ex As Exception
            Return False
            MsgBox("Error al autenticar usuario: " & ex.Message,
                   MsgBoxStyle.Exclamation)
        End Try

        Return True
    End Function

    Public Sub enfocar()
        SendKeys.Send("{ENTER}")
    End Sub

    Public Sub derecha()
        SendKeys.Send("{RIGHT}")
    End Sub

    Public Sub mensaje(ByVal men As String)
        MsgBox(men, MsgBoxStyle.Information, "Mensajes al Usuario")
    End Sub

End Class
